<?php 
require_once 'userManagement.php';
require_once 'restaurantManagement.php';
require_once 'settingsManagement.php';
require_once '../inc/database/DataBase.php';
require_once '../config.php';
$statement=null;

if (isset($_GET['p'])) {	  
  if($_GET['p']==1){
?>
<!DOCTYPE html>
<html>
<head>
  <title>Panel Administratora</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8; width=device-width, initial-scale=1.0" >
  <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-warning">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-danger">Podgląd zysków</a>
		  <a href="../" type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block">
		    <a href="profits.php?p=1" type="button" class="btn btn-danger btn-block" style="width: 270px;">Zyski</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-search form-inline" action="profits.php?p=1" method="POST">
			  <legend><center>Filtruj zyski</center></legend>
			  <center>			  
			    <select class="input" name="orderDate">
				  <option>Dzisiaj</option>
				  <option>Ostatni tydzień</option>
				  <option>Ostatni miesiąc</option>
				  <option>Ostatni rok</option>
				  <option>Wszystkie</option>
			    </select>
			  <button type="submit" class="btn-primary" name="filterProfits">Filtruj!</button>
			  </center>
			  <br><br>
			</form>
<?php
	$data=time();
	$newDate=null;
	if(isset($_REQUEST['filterProfits'])) {
		if(isset($_POST['orderDate'])){
			if($_POST['orderDate']=='Ostatni tydzień'){
				$newDate=date("Y-m-d",($data - (60*60*24*7)));
			}
			else if($_POST['orderDate']=='Ostatni miesiąc'){
				$newDate=date("Y-m-d",($data - (60*60*24*30)));
			}
			else if($_POST['orderDate']=='Ostatni rok'){
				$newDate=date("Y-m-d",($data - (60*60*24*365)));
			}
			else if($_POST['orderDate']=='Wszystkie'){
				$newDate=date("Y-m-d",($data - (60*60*24*365*12)));
			}
			else{
				$newDate=date("Y-m-d",$data); 
			}
		}
		echo('</div>
		  <div class="well">
			<form class="form-group" style="height: 290px">');
		$db = new DataBase();
		$config = new Config();
		$db->connect($config -> getHost(), $config -> getPort(), $config->getDbname(), $config->getUser(), $config->getPass());
		$statement=$db->query('select r.rname, o.profit, o.date from orders o, restaurants r where r.id=o.restaurant and o.date>'.$newDate);
		echo('<label class="col-sm-4 control-label"><h3>Nazwa restauracji</h3></label>');
		echo('<label class="col-sm-4 control-label"><h3><center>Zyski</h3></center></label>');
		echo('<label class="col-sm-4 control-label"><h3><center>Data operacji</h3></center></label><hr><br>');
		foreach($statement as $row)
      {
		echo('<label class="col-sm-4 control-label">'.$row['rname'].'</label>');
		echo('<label class="col-sm-4 control-label"><center>'.$row['profit'].'</center></label>');
		echo('<label class="col-sm-4 control-label"><center>'.substr($row['date'],0,10).'</center></label>');
      }
	  $statement->closeCursor();
	  echo('</form>
			  </div>');
	}
?>
		</div>
	</div>
  </div>
</body>
</html>
<?php
  }
}
?>